package com.uobgroup.ews.uam.web.taglib;

import java.security.SecureRandom;

import org.apache.commons.lang.ArrayUtils;

public class ExternalFunction {

   private static final String ROLE_UMM_SYSTEM_ADMINISTRATOR_CHECKER = "ROLE_UMM_SYSTEM_ADMINISTRATOR_CHECKER";

   private static final String ROLE_UMM_SYSTEM_ADMINISTRATOR_MAKER = "ROLE_UMM_SYSTEM_ADMINISTRATOR_MAKER";

   private static final String ROLE_APPLICATION_PARAMETER_ADMINISTRATOR_CHECKER = "ROLE_APPLICATION_PARAMETER_ADMINISTRATOR_CHECKER";

   private static final String ROLE_APPLICATION_PARAMETER_ADMINISTRATOR_MAKER = "ROLE_APPLICATION_PARAMETER_ADMINISTRATOR_MAKER";

   private static final String ROLE_SECURITY_PARAMETER_ADMINISTRATOR_CHECKER = "ROLE_SECURITY_PARAMETER_ADMINISTRATOR_CHECKER";

   private static final String ROLE_SECURITY_PARAMETER_ADMINISTRATOR_MAKER = "ROLE_SECURITY_PARAMETER_ADMINISTRATOR_MAKER";

   private static final String ROLE_PROFILE_ADMINISTRATOR_CHECKER = "ROLE_PROFILE_ADMINISTRATOR_CHECKER";

   private static final String ROLE_PROFILE_ADMINISTRATOR_MAKER = "ROLE_PROFILE_ADMINISTRATOR_MAKER";

   private static final String ROLE_USER_ID_ADMINISTRATOR_CHECKER = "ROLE_USER_ID_ADMINISTRATOR_CHECKER";

   private static final String ROLE_USER_ID_ADMINISTRATOR_MAKER = "ROLE_USER_ID_ADMINISTRATOR_MAKER";

   private static final String ROLE_UMM_AUDITOR = "ROLE_UMM_AUDITOR";

   private static final String ROLE_UMM_USER_ID_ENQUIRER = "ROLE_UMM_USER_ID_ENQUIRER";

   private static final String ROLE_APPLICATION_PARAMETER_ENQUIRER = "ROLE_APPLICATION_PARAMETER_ENQUIRER";

   private static String[] ProfileAdministrator = { "profiles", "accesscontrols", "uamroles", "combinationmatrixes", "rolesubtype" };

   private static String[] UserIdAdministrator = { "uamusers" };

   private static String[] SecurityParameterAdministrator = { "segments", "accesstostaffs", "securitygroups",
         "locationtransactions", "transactiondocuments", "countryprocessingcentres", "processingcentretransactions",
         "countrylocations", "references", "referencetypes" };

   private static String[] SystemAdministrator = { "applicationconfigurations" };

   private static String[] ApplicationParameterAdministrator = { "references" , "referencetypes"};

   private static String[] UMMAuditor = { "applicationconfigurations" };

   private static String[] UMMUserIdEnquirer = { "uamusers", "profiles", "accesscontrols", "uamroles",
         "combinationmatrixes", "rolesubtype", "segments", "accesstostaffs", "securitygroups", "locationtransactions",
         "transactiondocuments", "countryprocessingcentres", "processingcentretransactions", "countrylocations", "references", "referencetypes" };

   private static String[] ApplicationParameterEnquirer = { "references", "referencetypes" };

   private static String[] EnquiryAction = { "display", "list" };

   private static String[] CheckerAction = { "display", "list" };
   
   public static long getRandomNumber(){
      SecureRandom random = new SecureRandom();
      return random.nextLong();
   }

   public static boolean hasPermission(String strAccessMask, String strPermission) {
      if (null == strAccessMask || 0 == strAccessMask.trim().length())
         return false;

      int accessMask = Integer.parseInt(strAccessMask);
      int permission = Integer.parseInt(strPermission);
      boolean result = ((accessMask & permission) != 0);
      return result;
   }

   public static boolean isFunctionsOfRole(String action, String functionName, String roleNames) {
      if (null == functionName || 0 == functionName.trim().length() || null == roleNames
            || 0 == roleNames.trim().length()) {
         return false;
      }

      boolean result = false;
      String[] roles = roleNames.trim().split(",");
      for (int i = 0; i < roles.length; i++) {
         String roleName = roles[i];
         if (roleName.equals(ROLE_PROFILE_ADMINISTRATOR_CHECKER)
               && ArrayUtils.contains(ProfileAdministrator, functionName.trim())
               && ArrayUtils.contains(CheckerAction, action.trim())) {
            result = true;
            break;
         }
         else if (roleName.equals(ROLE_PROFILE_ADMINISTRATOR_MAKER)
               && ArrayUtils.contains(ProfileAdministrator, functionName.trim())) {
            result = true;
            break;
         }
         else if (roleName.equals(ROLE_USER_ID_ADMINISTRATOR_CHECKER)
               && ArrayUtils.contains(UserIdAdministrator, functionName.trim())
               && ArrayUtils.contains(CheckerAction, action.trim())) {
            result = true;
            break;
         }
         else if (roleName.equals(ROLE_USER_ID_ADMINISTRATOR_MAKER)
               && ArrayUtils.contains(UserIdAdministrator, functionName.trim())) {
            result = true;
            break;
         }
         else if (roleName.equals(ROLE_SECURITY_PARAMETER_ADMINISTRATOR_CHECKER)
               && ArrayUtils.contains(SecurityParameterAdministrator, functionName.trim())
               && ArrayUtils.contains(CheckerAction, action.trim())) {
            result = true;
            break;
         }
         else if (roleName.equals(ROLE_SECURITY_PARAMETER_ADMINISTRATOR_MAKER)
               && ArrayUtils.contains(SecurityParameterAdministrator, functionName.trim())) {
            result = true;
            break;
         }
         else if (roleName.equals(ROLE_APPLICATION_PARAMETER_ADMINISTRATOR_CHECKER)
               && ArrayUtils.contains(ApplicationParameterAdministrator, functionName.trim())
               && ArrayUtils.contains(CheckerAction, action.trim())) {
            result = true;
            break;
         }
         else if (roleName.equals(ROLE_APPLICATION_PARAMETER_ADMINISTRATOR_MAKER)
               && ArrayUtils.contains(ApplicationParameterAdministrator, functionName.trim())) {
            result = true;
            break;
         }
         else if (roleName.equals(ROLE_UMM_SYSTEM_ADMINISTRATOR_CHECKER)
               && ArrayUtils.contains(SystemAdministrator, functionName.trim())
               && ArrayUtils.contains(CheckerAction, action.trim())) {
            result = true;
            break;
         }
         else if (roleName.equals(ROLE_UMM_SYSTEM_ADMINISTRATOR_MAKER)
               && ArrayUtils.contains(SystemAdministrator, functionName.trim())) {
            result = true;
            break;
         }
         else if (roleName.equals(ROLE_UMM_AUDITOR) && ArrayUtils.contains(UMMAuditor, functionName.trim())
               && ArrayUtils.contains(EnquiryAction, action.trim())) {
            result = true;
            break;
         }
         else if (roleName.equals(ROLE_UMM_USER_ID_ENQUIRER)
               && ArrayUtils.contains(UMMUserIdEnquirer, functionName.trim())
               && ArrayUtils.contains(EnquiryAction, action.trim())) {
            result = true;
            break;
         }
         else if (roleName.equals(ROLE_APPLICATION_PARAMETER_ENQUIRER)
               && ArrayUtils.contains(ApplicationParameterEnquirer, functionName.trim())
               && ArrayUtils.contains(EnquiryAction, action.trim())) {
            result = true;
            break;
         }
         else {
            result = false;
         }
      }
      return result;
   }

   public static boolean isCheckerRole(String roleNames, String functionName) {
      if (null == roleNames || 0 == roleNames.trim().length()) {
         return false;
      }
      boolean result = false;
      String[] roles = roleNames.trim().split(",");
      for (int i = 0; i < roles.length; i++) {
         String roleName = roles[i];

         if (roleName.equals(ROLE_PROFILE_ADMINISTRATOR_CHECKER)
               && ArrayUtils.contains(ProfileAdministrator, functionName.trim())) {
            result = true;
            break;
         }
         else if (roleName.equals(ROLE_USER_ID_ADMINISTRATOR_CHECKER)
               && ArrayUtils.contains(UserIdAdministrator, functionName.trim())) {
            result = true;
            break;
         }
         else if (roleName.equals(ROLE_SECURITY_PARAMETER_ADMINISTRATOR_CHECKER)
               && ArrayUtils.contains(SecurityParameterAdministrator, functionName.trim())) {
            result = true;
            break;
         }
         else if (roleName.equals(ROLE_APPLICATION_PARAMETER_ADMINISTRATOR_CHECKER)
               && ArrayUtils.contains(ApplicationParameterAdministrator, functionName.trim())) {
            result = true;
            break;
         }
         else if (roleName.equals(ROLE_UMM_SYSTEM_ADMINISTRATOR_CHECKER)
               && ArrayUtils.contains(SystemAdministrator, functionName.trim())) {
            result = true;
            break;
         }
         else {
            result = false;
         }
      }
      return result;
   }

   public static boolean isMakerRole(String roleNames, String functionName) {
      if (null == roleNames || 0 == roleNames.trim().length()) {
         return false;
      }
      boolean result = false;
      String[] roles = roleNames.trim().split(",");
      for (int i = 0; i < roles.length; i++) {
         String roleName = roles[i];
         if (roleName.equals(ROLE_PROFILE_ADMINISTRATOR_MAKER)
               && ArrayUtils.contains(ProfileAdministrator, functionName.trim())) {
            result = true;
            break;
         }
         else if (roleName.equals(ROLE_USER_ID_ADMINISTRATOR_MAKER)
               && ArrayUtils.contains(UserIdAdministrator, functionName.trim())) {
            result = true;
            break;
         }
         else if (roleName.equals(ROLE_SECURITY_PARAMETER_ADMINISTRATOR_MAKER)
               && ArrayUtils.contains(SecurityParameterAdministrator, functionName.trim())) {
            result = true;
            break;
         }
         else if (roleName.equals(ROLE_APPLICATION_PARAMETER_ADMINISTRATOR_MAKER)
               && ArrayUtils.contains(ApplicationParameterAdministrator, functionName.trim())) {
            result = true;
            break;
         }
         else if (roleName.equals(ROLE_UMM_SYSTEM_ADMINISTRATOR_MAKER)
               && ArrayUtils.contains(SystemAdministrator, functionName.trim())) {
            result = true;
            break;
         }
         else {
            result = false;
         }
      }
      return result;
   }
}
